﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace deneme
{
    public partial class KislemleriForm : Form
    {
        SqlConnection con = new SqlConnection(ClassSqlConnection.ConnectionString);
        ClassShowDataGridView csdgw = new ClassShowDataGridView();
        ClassFindData cfd = new ClassFindData();




        public KislemleriForm()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (txt1.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Ad Bilgisi Giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (txt2.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Soyad Bilgisi giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (txt3.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Kullanici Ad Bilgisi giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (txt4.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Kullanici Sifre Bilgisi giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (cb1.Checked == false && cb2.Checked == false && cb3.Checked == false)
            {
                MessageBox.Show("Admin,PersonelSef ya da Personel  seciniz!");
                return;  // işlemi sona erdir. 
            }




            if (cb1.Checked == true)
            {

                int a = 1;
                con.Open();
                SqlCommand cmd = new SqlCommand("sp$KullaniciKayit", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@KullaniciTipID", a);
                cmd.Parameters.AddWithValue("@Ad", txt1.Text);
                cmd.Parameters.AddWithValue("@Soyad", txt2.Text);
                cmd.Parameters.AddWithValue("@KullaniciAd", txt3.Text);
                cmd.Parameters.AddWithValue("@KullaniciSifre", txt4.Text);
                int Kullanicivarmi;
                Kullanicivarmi = cmd.ExecuteNonQuery();
                if (Kullanicivarmi < 0)
                    MessageBox.Show("Bu Kullanici Adi kullaniliyor!");
                else
                    MessageBox.Show("Kayit İslemi Basariyla Tamamlandi");
                gridyenileme();
                con.Close();
                txt1.Text = "";
                txt2.Text = "";
                txt3.Text = "";
                txt4.Text = "";


            }
            if (cb2.Checked == true)
            {
                int a = 2;
                con.Open();
                SqlCommand cmd = new SqlCommand("sp$KullaniciKayit", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@KullaniciTipID", a);
                cmd.Parameters.AddWithValue("@Ad", txt1.Text);
                cmd.Parameters.AddWithValue("@Soyad", txt2.Text);
                cmd.Parameters.AddWithValue("@KullaniciAd", txt3.Text);
                cmd.Parameters.AddWithValue("@KullaniciSifre", txt4.Text);
                int Kullanicivarmi;
                Kullanicivarmi = cmd.ExecuteNonQuery();
                if (Kullanicivarmi < 0)
                    MessageBox.Show("Bu Kullanici Adi kullaniliyor!");
                else
                    MessageBox.Show("Kayit İslemi Basariyla Tamamlandi");
                gridyenileme();
                con.Close();
                txt1.Text = "";
                txt2.Text = "";
                txt3.Text = "";
                txt4.Text = "";


            }
            if (cb3.Checked == true)
            {
                int a = 3;
                con.Open();
                SqlCommand cmd = new SqlCommand("sp$KullaniciKayit", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@KullaniciTipID", a);
                cmd.Parameters.AddWithValue("@Ad", txt1.Text);
                cmd.Parameters.AddWithValue("@Soyad", txt2.Text);
                cmd.Parameters.AddWithValue("@KullaniciAd", txt3.Text);
                cmd.Parameters.AddWithValue("@KullaniciSifre", txt4.Text);
                int Kullanicivarmi;
                Kullanicivarmi = cmd.ExecuteNonQuery();
                if (Kullanicivarmi < 0)
                    MessageBox.Show("Bu Kullanici Adi kullaniliyor!");
                else
                    MessageBox.Show("Kayit İslemi Basariyla Tamamlandi");
                gridyenileme();
                con.Close();
                txt1.Text = "";
                txt2.Text = "";
                txt3.Text = "";
                txt4.Text = "";
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (txt5.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Kullanici Ad Bilgisi giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (txt6.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Kullanici Sifre Bilgisi giriniz!");
                return;  // işlemi sona erdir. 
            }

            con.Open();
            SqlCommand cmd = new SqlCommand("sp$KullaniciSil", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@KullaniciAd", txt5.Text);
            cmd.Parameters.AddWithValue("@KullaniciSifre", txt6.Text);
            int kvarmi;
            kvarmi = cmd.ExecuteNonQuery();
            if (kvarmi < 0)
                MessageBox.Show("Kullanici Bulunamadi!");
            else
                MessageBox.Show("Silme İslemi Basariyla Tamamlandi");
            txt5.Text = "";
            txt6.Text = "";
            gridyenileme();
            con.Close();
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (txt7.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Ad Bilgisi Giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (txt8.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Soyad Bilgisi giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (txt9.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Kullanici Ad Bilgisi giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (txt10.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Kullanici Sifre Bilgisi giriniz!");
                return;  // işlemi sona erdir. 
            }
            if (cb4.Checked == false && cb5.Checked == false && cb6.Checked == false)
            {
                MessageBox.Show("Admin,PersonelSef ya da Personel  seciniz!");
                return;  // işlemi sona erdir. 
            }
            if (cb4.Checked == true)
            {

                int a = 1;
                con.Open();
                SqlCommand cmd = new SqlCommand("sp$KullaniciGuncelle", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@KullaniciTipID", a);
                cmd.Parameters.AddWithValue("@Ad", txt7.Text);
                cmd.Parameters.AddWithValue("@Soyad", txt8.Text);
                cmd.Parameters.AddWithValue("@KullaniciAd", txt9.Text);
                cmd.Parameters.AddWithValue("@EskiSifre", txt10.Text);
                cmd.Parameters.AddWithValue("@YeniSifre", txt11.Text);
                int Kullanicivarmi;
                Kullanicivarmi = cmd.ExecuteNonQuery();
                if (Kullanicivarmi < 0)
                    MessageBox.Show("Kullanici Bulunamadi!");
                else
                    MessageBox.Show("Guncelleme İslemi Basariyla Tamamlandi");
                gridyenileme();
                con.Close();
                txt7.Text = "";
                txt8.Text = "";
                txt9.Text = "";
                txt10.Text = "";
                txt11.Text = "";


            }
            if (cb5.Checked == true)
            {
                int a = 2;
                con.Open();
                SqlCommand cmd = new SqlCommand("sp$KullaniciGuncelle", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@KullaniciTipID", a);
                cmd.Parameters.AddWithValue("@Ad", txt7.Text);
                cmd.Parameters.AddWithValue("@Soyad", txt8.Text);
                cmd.Parameters.AddWithValue("@KullaniciAd", txt9.Text);
                cmd.Parameters.AddWithValue("@EskiSifre", txt10.Text);
                cmd.Parameters.AddWithValue("@YeniSifre", txt11.Text);
                int Kullanicivarmi;
                Kullanicivarmi = cmd.ExecuteNonQuery();
                if (Kullanicivarmi < 0)
                    MessageBox.Show("Kullanici Bulunamadi!");
                else
                    MessageBox.Show("Guncelleme İslemi Basariyla Tamamlandi");
                gridyenileme();
                con.Close();
                txt7.Text = "";
                txt8.Text = "";
                txt9.Text = "";
                txt10.Text = "";
                txt11.Text = "";

            }
            if (cb6.Checked == true)
            {
                int a = 3;
                con.Open();
                SqlCommand cmd = new SqlCommand("sp$KullaniciGuncelle", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@KullaniciTipID", a);
                cmd.Parameters.AddWithValue("@Ad", txt7.Text);
                cmd.Parameters.AddWithValue("@Soyad", txt8.Text);
                cmd.Parameters.AddWithValue("@KullaniciAd", txt9.Text);
                cmd.Parameters.AddWithValue("@EskiSifre", txt10.Text);
                cmd.Parameters.AddWithValue("@YeniSifre", txt11.Text);
                int Kullanicivarmi;
                Kullanicivarmi = cmd.ExecuteNonQuery();
                if (Kullanicivarmi < 0)
                    MessageBox.Show("Kullanici Bulunamadi!");
                else
                    MessageBox.Show("Guncelleme İslemi Basariyla Tamamlandi");
                gridyenileme();
                con.Close();
                txt7.Text = "";
                txt8.Text = "";
                txt9.Text = "";
                txt10.Text = "";
                txt11.Text = "";
            }




        }

        private void btn4_Click(object sender, EventArgs e)
        {
            AdminForm af = new AdminForm();
            af.Show();
            this.Hide();
        }

        private void txt12_TextChanged(object sender, EventArgs e)
        {
            cfd.arama("tblKullanici", dataGridView1, "Ad", txt12);
        }

        private void txt13_TextChanged(object sender, EventArgs e)
        {
            cfd.arama("tblKullanici", dataGridView1, "Soyad", txt13);
        }

        private void txt14_TextChanged(object sender, EventArgs e)
        {
            cfd.arama("tblKullanici", dataGridView1, "KullaniciAd", txt14);
        }

        private void KislemleriForm_Load(object sender, EventArgs e)
        {
            dataGridView1.BackgroundColor = this.BackColor;
            gridyenileme();
        }

        private void cb1_CheckedChanged(object sender, EventArgs e)
        {
            if (cb1.Checked == true)
            {
                cb2.Enabled = false;
                cb3.Enabled = false;
            }
            if (cb1.Checked == false)
            {
                cb2.Enabled = true;
                cb3.Enabled = true;
            }
        }

        private void cb2_CheckedChanged(object sender, EventArgs e)
        {
            if (cb2.Checked == true)
            {
                cb1.Enabled = false;
                cb3.Enabled = false;
            }
            if (cb2.Checked == false)
            {
                cb1.Enabled = true;
                cb3.Enabled = true;
            }
        }

        private void cb3_CheckedChanged(object sender, EventArgs e)
        {
            if (cb3.Checked == true)
            {
                cb1.Enabled = false;
                cb2.Enabled = false;
            }
            if (cb3.Checked == false)
            {
                cb1.Enabled = true;
                cb2.Enabled = true;
            }
        }

        private void cb4_CheckedChanged(object sender, EventArgs e)
        {
            if (cb4.Checked == true)
            {
                cb5.Enabled = false;
                cb6.Enabled = false;
            }
            if (cb4.Checked == false)
            {
                cb5.Enabled = true;
                cb6.Enabled = true;
            }

        }

        private void cb5_CheckedChanged(object sender, EventArgs e)
        {
            if (cb5.Checked == true)
            {
                cb4.Enabled = false;
                cb6.Enabled = false;
            }
            if (cb5.Checked == false)
            {
                cb4.Enabled = true;
                cb6.Enabled = true;
            }
        }

        private void cb6_CheckedChanged(object sender, EventArgs e)
        {
            if (cb6.Checked == true)
            {
                cb4.Enabled = false;
                cb5.Enabled = false;
            }
            if (cb6.Checked == false)
            {
                cb4.Enabled = true;
                cb5.Enabled = true;
            }
        }
        public void gridyenileme()  //datagridimizi refresh etmek için method
        {
            String cmd = "SELECT Ad, Soyad, KullaniciAd, KullaniciSifre, KayitTarih FROM dbo.tblKullanici";
            csdgw.loadDataGrid(dataGridView1, cmd);

        }
    }
}
